package com.site.core.proxy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import com.cnc.bas.util.Log;

/**
 * 代理管理器
 * @author Administrator
 *
 */
public class ProxyManager extends Thread{
	
	private Log log = Log.getLogger(this.getClass());
	
	// 含有最新代理的文件
	private Set<String> proxyFiles = new HashSet<String>();
	
	// 存放代理
	private List<Proxy> proxys = new ArrayList<Proxy>();
	
	private static ProxyManager instance = new ProxyManager();
	
	public static ProxyManager getInstance(){
		return instance;
	}
	
	private ProxyManager(){
		
	}
	
	public void run(){
		while(true){
			try{
				// 刷新代理信息
				refresh();
			}
			catch(Exception e){
				log.logError("refresh proxy exception:" + e.getMessage(), e);
			}
			
			try{
				Thread.sleep(60 * 1000);
			}
			catch(Exception e){
			}
		}
	}
	
	private void refresh(){
		log.logInfo("begin refresh proxys.");
		loadProxyFiles(proxyFiles);
		log.logInfo("refresh proxys finish.");
	}
	
	/**
	 * 获取一个可用的代理
	 * @return
	 */
	public Proxy getUseableProxy(){
		synchronized (proxys) {
			while(true){
				int size = proxys.size();
				if(size == 0){
					break;
				}
				Random rd = new Random();
				int index = rd.nextInt(size);
				Proxy proxy = proxys.get(index);
				if(ProxyChecker.isUsable(proxy)){
					log.logInfo("find alive proxy:" + proxy + ".");
					return proxy;
				}
				else{
					proxys.remove(index);
				}
			}
		}
		return null;
	}
	
	
	
	private void loadProxyFiles(Collection<String> files){
		Set<Proxy> result = new HashSet<Proxy>();
		if(files == null || files.isEmpty()){
			log.logError("no proxy files, please add proxy files!");
			return;
		}
		for(String file : files){
			Collection<Proxy> data = loadProxyFromFile(file);
			if(data != null){
				result.addAll(data);
			}
		}
		synchronized (proxys) {
			proxys.clear();
			proxys.addAll(result);
		}
	}
	
 	private Collection<Proxy> loadProxyFromFile(String fileName){
 		try{
 			File file = new File(fileName);
 			if(!file.exists()){
 				log.logError("proxy file:" + fileName + " is not exist!");
 				return null;
 			}
 			Collection<Proxy> proxys = new HashSet<Proxy>();
 			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
 			String line = null;
 			while((line=br.readLine()) != null){
 				Proxy proxy = parseProxy(line);
 				if(proxy != null){
 					proxys.add(proxy);
 				}
 			}
 			return proxys;
 		}
 		catch(Exception e){
 			log.logError("proxy file parse exception:" + e.getMessage(), e);
 		}
 		return null;
	}
 	
 	/**
 	 * 从文本中解悉出代理信息
 	 * @param proxyStr
 	 * @return
 	 */
 	private Proxy parseProxy(String proxyStr){
 		return null;
 	}

}
